home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 16 / PC Actual CD 16.iso / cdactual / Os2 / 0509 / FreeFormEdit.java < prev    next >
Encoding:
Java Source  |  1997-08-19  |  5.7 KB  |  222 lines

  1. package macrolanguage;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import macrolanguage.EditToolbar;
  5. import macrolanguage.NotAContainerException;
  6.  
  7. public class FreeFormEdit extends Frame
  8.       implements  KeyListener, WindowListener
  9. {
  10.   /**
  11.    * The constructor creates the listeners with the correct layout
  12.    * for the free form design.
  13.   **/
  14.   public FreeFormEdit()
  15.   {
  16.     super();
  17.  
  18.     addWindowListener(this);
  19.     addKeyListener(this);
  20.     addMouseListener(mouseManager);
  21.  
  22.     setLayout(new FlowLayout());
  23.  
  24.     // default size.
  25.     setSize(100,100);
  26.  
  27.     show();
  28.   }
  29.  
  30.   /**
  31.    * Add a component to an x and y location.
  32.    * This method will be called by the user putting a component on
  33.    * the form or an embedded container. This method throws a
  34.    * NotAContainerException when the x and y location points at a
  35.    * component, since components cannot be placed on other components
  36.    * in Java (this is untrue with regarding lightweight components).
  37.   **/
  38.   public void add(Component c, int x, int y) throws NotAContainerException
  39.   {
  40.     // Check whether there is a component at the x and y location
  41.     // and if so throw NotAContainerException.
  42.     Component temp = getComponentAt(x,y);
  43.     if(!(temp instanceof Container))
  44.       throw (new NotAContainerException("Tried to add a component to something other than a container."));
  45.  
  46.     // Add listeners to events regarding these componenets.
  47.     // The listening is performed in the MouseManager class to avoid
  48.     // bloating this class.
  49.     c.addMouseMotionListener(mouseManager);
  50.     c.addMouseListener(mouseManager);
  51.  
  52.     // Add the component to the container.
  53.     ((Container)temp).add(c);
  54.  
  55.     // Set the relative x and y location.
  56.     c.setLocation(x, y);
  57.   }
  58.  
  59.   /**
  60.    * Set the selection Component to be the selected Component.
  61.   **/
  62.   public void setSelection(Component selection)
  63.   {
  64.     if (this.selection != selection)
  65.     {
  66.       this.selection = selection;
  67.       repaint();
  68.     }
  69.   }
  70.  
  71.   /**
  72.    * Places the component comp on the next click on the free form
  73.    * edit surface.
  74.   **/
  75.   public void placeComponentOnNextClick(Component comp)
  76.   {
  77.     // Change the cursor so it will be evident we are in the middle
  78.     // of a drop operation.
  79.     setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
  80.     mouseManager.placeComponentOnNextClick(comp);
  81.   }
  82.  
  83.   /**
  84.    *
  85.   **/
  86.   public void removeSelection()
  87.   {
  88.     if(selection != this)
  89.     {
  90.       remove(selection);
  91.       selection = this;
  92.       repaint();
  93.     }
  94.   }
  95.  
  96.   /**
  97.    * This method is a part of the keylistener interface used
  98.    * to listen to keyboard events.
  99.   **/
  100.   public void keyTyped(java.awt.event.KeyEvent ev)
  101.   {
  102.   }
  103.  
  104.   /**
  105.    * This method is a part of the keylistener interface used
  106.    * to listen to keyboard events.
  107.   **/
  108.   public void keyPressed(java.awt.event.KeyEvent ev)
  109.   {
  110.   }
  111.  
  112.   /**
  113.    * This method is a part of the keylistener interface used
  114.    * to listen to keyboard events.
  115.   **/
  116.   public void keyReleased(java.awt.event.KeyEvent ev)
  117.   {
  118.   }
  119.  
  120.   /**
  121.    * This method is a part of the Windowlistener interface used
  122.    * to listen to window events.
  123.   **/
  124.   public void windowOpened(java.awt.event.WindowEvent ev)
  125.   {
  126.   }
  127.  
  128.   /**
  129.    * This method is a part of the Windowlistener interface used
  130.    * to listen to window events.
  131.   **/
  132.   public void windowClosing(java.awt.event.WindowEvent ev)
  133.   {
  134.     System.exit(0);
  135.   }
  136.  
  137.   /**
  138.    * This method is a part of the Windowlistener interface used
  139.    * to listen to window events.
  140.   **/
  141.   public void windowClosed(java.awt.event.WindowEvent ev)
  142.   {
  143.   }
  144.  
  145.   /**
  146.    * This method is a part of the Windowlistener interface used
  147.    * to listen to window events.
  148.   **/
  149.   public void windowIconified(java.awt.event.WindowEvent ev)
  150.   {
  151.   }
  152.  
  153.   /**
  154.    * This method is a part of the Windowlistener interface used
  155.    * to listen to window events.
  156.   **/
  157.   public void windowDeiconified(java.awt.event.WindowEvent ev)
  158.   {
  159.   }
  160.  
  161.   /**
  162.    * This method is a part of the Windowlistener interface used
  163.    * to listen to window events.
  164.   **/
  165.   public void windowActivated(java.awt.event.WindowEvent ev)
  166.   {
  167.   }
  168.  
  169.   /**
  170.    * This method is a part of the Windowlistener interface used
  171.    * to listen to window events.
  172.   **/
  173.   public void windowDeactivated(java.awt.event.WindowEvent ev)
  174.   {
  175.   }
  176.  
  177.  
  178.   /**
  179.    * The paint method draws the marking around the selection.
  180.   **/
  181.   public void paint(Graphics g)
  182.   {
  183.     super.paint(g);
  184.  
  185.     // If the frame is selected do nothing.
  186.     if(selection != this)
  187.     {
  188.       // If the selection is not in this container.
  189.       if(selection.getParent() != this)
  190.         // Draw little squares around the four corners of the selected component.
  191.         drawSelection(selection,
  192.                       selection.getParent().getGraphics());
  193.       else
  194.         // Draw little squares around the four corners of the selected component.
  195.         drawSelection(selection, g);
  196.     }
  197.   }
  198.  
  199.   /**
  200.    * Draw a thin line around the selected component.
  201.    **/
  202.   private void drawSelection(Component select, Graphics g)
  203.   {
  204.     g.setColor(Color.black);
  205.  
  206.     g.fillRect(select.getLocation().x - SQUARE_SIZE,
  207.                select.getLocation().y - SQUARE_SIZE,
  208.                select.getSize().width + SQUARE_SIZE,
  209.                select.getSize().height + SQUARE_SIZE);
  210.   }
  211.  
  212.   // The currently selected component. By default this frame.
  213.   private Component selection = this;
  214.  
  215.   // The MouseManager takes over the full responsibility of manageing the
  216.   // mouse events for the form designing.
  217.   private MouseManager mouseManager = new MouseManager(this);
  218.  
  219.   // The default width of the line surrounding selected components.
  220.   private static final int SQUARE_SIZE = 2;
  221. }
  222.